package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class GaoJiaoSheJsonParse {

    @Autowired
    ICxgBookService bookService;


    @Test
    public void doParseJson() {
        FileReader fileReader = new FileReader("D:\\Spider\\GaoJiaoShuYuan\\3.json");
        String result = fileReader.readString();
        JSONArray objectArray = JSON.parseArray(result);
        Integer num = 0;
        for (int i = 0; i < objectArray.size(); i++) {
            JSONObject jsonObject = objectArray.getJSONObject(i);
            JSONObject data = jsonObject.getJSONObject("Data");
            JSONArray itemList = data.getJSONArray("ItemList");
            for (int j = 0; j < itemList.size(); j++) {
                JSONObject jsonObject1 = itemList.getJSONObject(j);
                String abstracts = jsonObject1.getString("Abstracts");
                String title = jsonObject1.getString("Title");
                String pkId = jsonObject1.getString("Id");
                String digitalUri = "https://ebook.hep.com.cn/ebooks/index.html#/read?id=" + pkId;
                String coverUrl = jsonObject1.getString("CoverUrl");
                String author = jsonObject1.getString("Author");
                JSONObject extendData = jsonObject1.getJSONObject("ExtendData");
                String isbn = extendData.getString("Isbn");
                if (StrUtil.isBlank(isbn)) {
                    continue;
                }
                isbn = isbn.replaceAll("-", "").replaceAll("—", "");
                CxgBook bookEntity = new CxgBook();
                bookEntity.setBrief(abstracts);
                bookEntity.setBookName(title);
                bookEntity.setDigitalUri(digitalUri);
                bookEntity.setPkId(pkId);
                bookEntity.setCover(coverUrl);
                bookEntity.setAuthor(author);
                bookEntity.setBookIsbn(isbn);
                bookEntity.setIsSync(0);
                bookEntity.setCreateTime(LocalDateTime.now());
                bookEntity.setPressName("高等教育出版社");
                bookEntity.setStatus(1);
                bookEntity.setPressId(269);
                num++;
                log.info(bookEntity.getBookName());
                log.info("num:"+num);
                Integer count = bookService.query().eq(CxgBook::getBookIsbn, isbn).count();
                if (count == 0) {
                    bookService.save(bookEntity);
                }
            }
        }
        log.info("numAll:"+num);

    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
